#!/bin/sh
#
# Start/stop/restart the syslog-ng daemon
# Drop-in replacement for a stock Slackware rc.syslog script
# Written by Mario Preksavec <mario at slackware dot hr>
# Updated by Janos Szigetvari <jszigetvari at gmail dot com>

syslog_start() {
  if [ -x /usr/sbin/syslog-ng ]; then
    if [ -f /var/run/syslog-ng.pid ] && kill -0 $(cat /var/run/syslog-ng.pid) 2> /dev/null; then
      echo "syslog-ng daemon already running!" >&2
      return
    fi

    echo "Starting syslog-ng daemon:  /usr/sbin/syslog-ng"
    /usr/sbin/syslog-ng -R /var/lib/syslog-ng/syslog-ng.persist

    # Link syslogd.pid for compatibility with other rc scripts
    if [ -f /var/run/syslog-ng.pid ] && kill -0 $(cat /var/run/syslog-ng.pid) 2> /dev/null; then
      ln -sf /var/run/syslog-ng.pid /var/run/syslogd.pid
    fi
  fi
}

syslog_stop() {
  if [ -f /var/run/syslog-ng.pid ]; then
    echo "Stopping syslog-ng daemon"
    kill $(cat /var/run/syslog-ng.pid) 2> /dev/null && rm -f /var/run/syslog-ng.pid
    # Unlink syslogd.pid
    unlink /var/run/syslogd.pid
  fi
  killall syslog-ng 2> /dev/null
}

syslog_restart() {
  syslog_stop
  sleep 1
  syslog_start
}

syslog_reload() {
  if [ -f /var/run/syslog-ng.pid ] && kill -0 $(cat /var/run/syslog-ng.pid) 2> /dev/null; then
    /usr/sbin/syslog-ng-ctl reload
  else
    echo "syslog-ng daemon is not running!" >&2
  fi
}

syslog_debug() {
  if [ -f /var/run/syslog-ng.pid ] && kill -0 $(cat /var/run/syslog-ng.pid) 2> /dev/null; then
    /usr/sbin/syslog-ng-ctl verbose --set="$1"
    /usr/sbin/syslog-ng-ctl debug --set="$1"
    /usr/sbin/syslog-ng-ctl trace --set="$1"
  else
    echo "syslog-ng daemon is not running!" >&2
  fi
}

case "$1" in
'start')
  syslog_start
  ;;
'stop')
  syslog_stop
  ;;
'restart')
  syslog_restart
  ;;
'reload')
  syslog_reload
  ;;
'enable-debug')
  syslog_debug on
  ;;
'disable-debug')
  syslog_debug off
  ;;
*)
  echo "usage $0 start|stop|reload|restart|enable-debug|disable-debug"
esac

